package com.ngc.mall.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ngc.mall.domain.CategoryDomain;
import com.ngc.mall.domain.CouponDomain;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface CouponMapper extends BaseMapper<CouponDomain> {


    @Select("select * from coupon where id in (select coupon_id from coupon_category where category_id = #{cid}) and start_date<#{now} and end_date>#{now}")
    List<CouponDomain> findByCategoryId(@Param("cid") Long cid, @Param("now")LocalDateTime now);

    @Insert({
            "<script>",
            "insert into coupon_category(category_id,coupon_id) values",
            "<foreach collection='categoryIdList' item='item'  separator=',' >",
            "(#{item},#{couponId})",
            "</foreach>",
            "</script>"
    })
    void insertCouponCategories(@Param("categoryIdList") List<Long> categoryIdList,@Param("couponId") Long couponId);

    @Select({"select * from product_category where id in (select category_id from coupon_category where coupon_id =#{cid})"})
    List<CategoryDomain> findCategoryListByCouponId(Long cid);

}
